package com.fla.aiba.service.impl;

import com.fla.aiba.repository.*;
import com.fla.aiba.service.UserService;
import com.fla.aiba.dto.UserDto;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qudingyi on 2017/4/24.
 */
@Service
public class UserServiceImpl implements UserService {
    private    String USER_VIE_SQL="SELECT u.id,u.age,u.avatar,u.nick_name as nickName,u.sex," +
            " u.signature,usp.work_address as workAddress,usp.occupation,usp.height ,usp.education from t_user  u" +
            " LEFT JOIN t_user_profile usp  ON  usp.id=u.id";

    private static final String LIVE_USER_VIEW_SQL="select \n" +
            "u.age,\n" +
            "u.avatar,\n" +
            "u.geohash,\n" +
            "u.id as userId,\n" +
            "u.nick_name as nickName,\n" +
            "u.login_city as city,\n" +
            "u.birthday,\n" +
            "u.mobile,\n" +
            "le.live_id as liveId,\n" +
            "COUNT(lf.id) as myfollowNumber,\n" +
            "COUNT(lfm.id) as foolowNumber\n" +
            "from t_user u\n" +
            "LEFT JOIN t_live_follow lfm on lfm.follow_use_id=u.id\n" +
            "LEFT JOIN t_live_follow lf on lf.user_id=u.id\n" +
            "LEFT JOIN t_live le on le.user_id=u.id\n" +
            "LEFT JOIN t_user_wallet uw on uw.user_id=u.id\n" +
            "GROUP BY u.id ";

    @Autowired
    private BaseNativeSqlRepository baseNativeSqlRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserProfileRepository userProfileRepository;
    @Autowired
    private MatechoiceRepository matechoiceRepository;
    @Autowired
    private PhotoAlbumRepository photoAlbumRepository;
    @Autowired
    private LiveRepository liveRepository;
    @Autowired
    private WalletRepository walletRepository;
    @PersistenceUnit
    private EntityManagerFactory emf;


    @Override
    public UserDto getLiveUserInfo(String userId) {
       EntityManager em=emf.createEntityManager();
       StringBuffer buffer=new StringBuffer("select v.* from (");
       buffer.append(LIVE_USER_VIEW_SQL);
       buffer.append(") as v ");
       buffer.append("where v.userId=:userId ");
       Query nativeQuery = em.createNativeQuery(buffer.toString());
        nativeQuery.setParameter("userId",userId);
       SQLQuery sqlQuery= nativeQuery.unwrap(SQLQuery.class);
       sqlQuery.addScalar("myfollowNumber", StandardBasicTypes.INTEGER);
       sqlQuery.addScalar("foolowNumber", StandardBasicTypes.INTEGER);
       sqlQuery.addScalar("liveId", StandardBasicTypes.LONG);
       nativeQuery.unwrap(SQLQuery.class).setResultTransformer(Transformers.aliasToBean( UserDto.class));
        UserDto view= (UserDto) nativeQuery.getSingleResult();
       em.close();
       return view;
    }

    @Override
    public Map<String, List<Map<String,Object>>> loadMemberView(int sex) {
        List<Map<String,Object>> everyDayMember=baseNativeSqlRepository.sqlListMap(USER_VIE_SQL+"  where u.sex="+sex +" LIMIT 0,6");
        List<Map<String,Object>>member=baseNativeSqlRepository.sqlListMap(USER_VIE_SQL+"  where u.sex="+sex);
        List<Map<String,Object>> cityMember=baseNativeSqlRepository.sqlListMap(USER_VIE_SQL+"  where u.sex="+sex);
        Map<String,List<Map<String,Object>>> maps=new HashMap<>();
        maps.put("everyDays",everyDayMember);
        maps.put("cityMember",cityMember);
        maps.put("members",member);
        return maps;
    }
}
